home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / osrc.arc / AUDIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-12  |  2.4 KB  |  111 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4.  
  5. extern char _Uend;
  6. extern int _STKRED;
  7.  
  8. union header {
  9.     struct {
  10.         union header *ptr;
  11.         unsigned size;
  12.     } s;
  13.     long l;
  14. };
  15. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  16.  * nonzero otherwise
  17.  */
  18. audit(bp,file,line)
  19. struct mbuf *bp;
  20. char *file;
  21. int line;
  22. {
  23.     register struct mbuf *bp1;
  24.  
  25.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  26.         audit_mbuf(bp1,file,line);
  27. }
  28. audit_mbuf(bp,file,line)
  29. register struct mbuf *bp;
  30. char *file;
  31. int line;
  32. {
  33.     union header *blk;
  34.     char *bufstart,*bufend;
  35.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  36.     int16 datasize;
  37.     int errors = 0;
  38.     char *heapbot,*heaptop;
  39.  
  40.     if(bp == NULLBUF)
  41.         return;
  42.  
  43.     heapbot = &_Uend;
  44.     heaptop = (char *) -_STKRED;
  45.  
  46.     /* Does buffer appear to be a valid malloc'ed block? */
  47.     blk = ((union header *)bp) - 1;
  48.     if(blk->s.ptr != blk){
  49.         printf("Garbage bp %lx\n",(long)bp);
  50.         errors++;
  51.     }
  52.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  53.         /* mbuf has data area associated with it, verify that
  54.          * pointers are within it
  55.          */
  56.         bufstart = (char *)(bp + 1);
  57.         bufend = (char *)bufstart + datasize;
  58.         if(bp->data < bufstart){
  59.             printf("Data pointer before buffer\n");
  60.             errors++;
  61.         }
  62.         if(bp->data + bp->cnt > bufend){
  63.             printf("Data pointer + count past bounds\n");
  64.             errors++;
  65.         }
  66.     } else {
  67.         /* Dup'ed mbuf, at least check that pointers are within
  68.          * heap area
  69.         */
  70.  
  71.         if(bp->data < heapbot
  72.          || bp->data + bp->cnt > heaptop){
  73.             printf("Data outside heap\n");
  74.             errors++;
  75.         }
  76.     }
  77.     /* Now check link list pointers */
  78.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  79.          || bp->next > (struct mbuf *)heaptop)){
  80.             printf("next pointer out of limits\n");
  81.             errors++;
  82.     }
  83.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  84.          || bp->anext > (struct mbuf *)heaptop)){
  85.             printf("anext pointer out of limits\n");
  86.             errors++;
  87.     }
  88.     if(errors != 0){
  89.         dumpbuf(bp);
  90.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  91.         fflush(stdout);
  92.         for(;;)
  93.             ;
  94.     }
  95.     return;
  96. }
  97. dumpbuf(bp)
  98. struct mbuf *bp;
  99. {
  100.     union header *blk;
  101.     if(bp == NULLBUF){
  102.         printf("NULL BUFFER\n");
  103.         return;
  104.     }
  105.     blk = ((union header *)bp) - 1;
  106.     printf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  107.         (long)bp,blk->s.size * sizeof(union header),
  108.         (long)bp->data,bp->cnt,
  109.         (long)bp->next,(long)bp->anext);
  110. }
  111.